/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.chigs.web.admin.version;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.chigs.domain.Version;
import com.googlecode.chigs.repository.GenericDomainService;
import com.googlecode.chigs.web.common.ActionPanelBuilder;
import com.googlecode.chigs.web.common.DeleteActionLink;
import com.googlecode.chigs.web.common.EditActionLink;
import com.googlecode.chigs.web.common.IconFactory;
import com.googlecode.chigs.web.common.table.AjaxFallbackDefaultDataTableBuilder;
import com.googlecode.chigs.web.common.table.DefaultDataProvider;

/**
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 */
@AuthorizeInstantiation("ROLE_ADMIN")
public class ManageVersionsPanel extends Panel {
	@SpringBean
	private GenericDomainService genericDomainService;

	public ManageVersionsPanel(String id) {
		super(id);
		Link<Void> createLink = new Link<Void>("create") {
			@Override
			public void onClick() {
				EditOrCreateVersionPanel edit = new EditOrCreateVersionPanel(ManageVersionsPanel.this.getId());
				edit.setOutputMarkupId(true);
				getPage().addOrReplace(edit);
				this.setVisible(false);
			}
		};
		createLink.add(new Image("icon", IconFactory.createAddApplicationsIconReference()));
		add(createLink);
		add(getVersionsTable("table"));
	}

	@SuppressWarnings("unchecked")
	private Component getVersionsTable(String id) {
		return AjaxFallbackDefaultDataTableBuilder.getBuilder(ManageVersionsPanel.this)
		.addDataProvider(new DefaultDataProvider<Version>(new Version()))
		.add(new AbstractColumn(new StringResourceModel("version.actions.label", ManageVersionsPanel.this, null)) {
			@Override
			public void populateItem(Item cellItem, String componentId, IModel rowModel) {
				final ActionPanelBuilder linkBuilder = ActionPanelBuilder.getBuilder();
				final IModel<Version> versionModel = new Model((Version) rowModel.getObject());
				// Eintrag editieren
				linkBuilder.add(new EditActionLink(false) {
					@Override
					public void onClick(AjaxRequestTarget target) {
						EditOrCreateVersionPanel edit = new EditOrCreateVersionPanel(ManageVersionsPanel.this.getId(), versionModel);
						edit.setOutputMarkupId(true);
						ManageVersionsPanel.this.getParent().addOrReplace(edit);
					}
				});
				DeleteActionLink removeLink = new DeleteActionLink(new StringResourceModel("version.actions.delete.confirm", ManageVersionsPanel.this, null), false) {
					@Override
					public void onClick(AjaxRequestTarget target) {
						genericDomainService.delete(Version.class, versionModel.getObject());
					}
				};
				linkBuilder.add(removeLink);
				cellItem.add(linkBuilder.build(componentId));
			}
		})				
		.addPropertyColumn("id", true)
		.addPropertyColumn("major", true)
		.addPropertyColumn("minor", true)
		.addPropertyColumn("variant.name", true)
		.addPropertyColumn("product.name", true)
		.setNumberOfRows(10)
		.build(id);
	}
}

